package cn.maihe.elg.operation.centers.epoint.channel.service;

import cn.maihe.elg.operation.centers.epoint.channel.EpointBaseChannelService;
import cn.maihe.elg.operation.centers.epoint.dto.channel.EpointDownloadPublickeyReqDTO;
import cn.maihe.elg.operation.centers.epoint.dto.channel.EpointDownloadPublickeyRespDTO;
import cn.maihe.elg.operation.centers.epoint.enums.EpointChannelMethod;
import cn.maihe.elg.operation.centers.epoint.enums.EpointRespCodeEnum;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import cn.maihe.elg.operation.utils.ValidateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description 金融服务系统作为服务方，金融机构作为请求方。
 * 金融机构请求金融服务系统，下载公钥信息。
 * @Author WJH
 * @Date 2021/04/21
 */
@Service
@Slf4j
public class NPEpointDownloadPublickeyChannelService extends EpointBaseChannelService<EpointDownloadPublickeyReqDTO, EpointDownloadPublickeyRespDTO> {

    @Resource
    private ElgResourceService elgResourceService;

    @Override
    public EpointChannelMethod methodEnum() {
        return EpointChannelMethod.downloadpublickey;
    }

    public InnerBaseResp downloadPublickey(String acceptOrderNo, CenterNoEnum centerNoEnum, EpointDownloadPublickeyReqDTO reqDTO) {
        InnerBaseResp<String> innerBaseResp = null;

        try {
            ValidateUtil.validate(reqDTO);
        } catch (Exception e) {
            innerBaseResp = InnerBaseResp.error("下载公钥参数有误:" + e.getMessage());
            return innerBaseResp;
        }
        try {
            EpointDownloadPublickeyRespDTO resp = this.doRequest( centerNoEnum, reqDTO);
            if (EpointRespCodeEnum.isSuccess(resp.getCode())) {
                innerBaseResp = InnerBaseResp.success(resp.getData().getPublickey());
            } else {
                innerBaseResp = InnerBaseResp.error(resp.getMessage());
            }
        } catch (Exception e) {
            log.error("下载公钥 >> {}: [{}] 异常:{}", acceptOrderNo,centerNoEnum.getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("下载公钥异常:" + e.getMessage());
        }
        return innerBaseResp;
    }

}
